package stackqueue;

import java.util.Stack;

/**
 * @Author: 海琳琦
 * @Date: 2022/2/8 16:07
 * https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/
 */
public class EvalRPN {

    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            if ("+".equals(tokens[i])||"-".equals(tokens[i])||"*".equals(tokens[i])||"/".equals(tokens[i])) {
                Integer pop = stack.pop();
                Integer pop1 = stack.pop();
                Integer result;
                if ("+".equals(tokens[i])) {
                    result = pop1 + pop;
                } else if ("-".equals(tokens[i])) {
                    result = pop1 - pop;
                } else if ("*".equals(tokens[i])) {
                    result = pop1 * pop;
                }else{
                    result = pop1 / pop;
                }
                stack.push(result);
            }else{
                stack.push(Integer.valueOf(tokens[i]));
            }
        }
        return stack.peek();
    }

    public static void main(String[] args) {

    }
}
